﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowlegeAssistant
{
    class ThemeUtil
    {
        public static List<Theme> GetAllThemes()
        {
            List<Theme> themes = GetAllThemes(0);

            return themes;
        }

        public static Theme GetTheme(int theme_id)
        {
            Theme theme = null;

            DataTable themestable = new DataTable();
            themestable = DatabaseConnect.execSP("GetTheme", new List<string>() { theme_id.ToString() }).Tables[0];

            Subject s = null;

            if (themestable.Rows.Count != 0)
            {
                s = SubjectUtil.GetSubject((int)themestable.Rows[0]["subject_id"]);

                theme = new Theme(
                        (int)themestable.Rows[0]["theme_id"],
                        s,
                        themestable.Rows[0]["name"].ToString()
                );
            }

            return theme;
        }

        public static List<Theme> GetAllThemes(int subject_id)
        {
            List<Theme> themes = new List<Theme>() { };

            DataTable themestable = new DataTable();
            List<string> parameters = new List<string>() { subject_id.ToString() };
            themestable = DatabaseConnect.execSP("GetAllThemes", parameters).Tables[0];

            Theme t = null;
            Subject s = null;

            foreach (DataRow r in themestable.Rows)
            {
                s = SubjectUtil.GetSubject((int)r["subject_id"]);

                t = new Theme(
                        (int)r["theme_id"],
                        s,
                        r["name"].ToString()
                );

                t.CountOfFirstQuestions = (int)r["count_of_first_questions"];
                t.CountOfSecondQuestionsType1 = (int)r["count_of_second_questions_type1"];
                t.CountOfSecondQuestionsType2 = (int)r["count_od_second_questions_type2"];
                t.Duration = (int)r["duration_time"];
                t.Three = (int)r["three"];
                t.Four = (int)r["four"];
                t.Five = (int)r["five"];


                themes.Add(t);
            }

            return themes;
        }

        public static int AddTheme(string theme_name, Subject subject)
        {
            int theme_id = 0;
            DataTable table = DatabaseConnect.execSP("CreateTheme", 
                new List<string>() { theme_name, subject.SubjectID.ToString() }).Tables[0];
            theme_id = int.Parse(table.Rows[0]["theme_id"].ToString());
            return theme_id;
        }

        public static void UpdateTheme(int theme_id, string theme_name, Subject subject, int count_of_first_questions,
            int count_of_second_questions_type1, int count_of_second_questions_type2, int duration_time,
            int three, int four, int five)
        {
            List<string> parameters = new List<string>();
            parameters.Add(theme_id.ToString());
            parameters.Add(subject.SubjectID.ToString() );
            parameters.Add(theme_name);
            parameters.Add(count_of_first_questions.ToString());
            parameters.Add(count_of_second_questions_type1.ToString());
            parameters.Add(count_of_second_questions_type2.ToString());
            parameters.Add(duration_time.ToString());

            parameters.Add(three.ToString());
            parameters.Add(four.ToString());
            parameters.Add(five.ToString());

            DatabaseConnect.execSPNonQuery("UpdateTheme", parameters);
        }

        public static void Removetheme(int theme_id)
        {
            DatabaseConnect.execSPNonQuery("RemoveTheme", new List<string>() { theme_id.ToString() });
        }

        public static void CopyQuestions(Theme src, Theme dest)
        {
            List<string> parameters = new List<string>();
            parameters.Add(src.ThemeID.ToString());
            parameters.Add(dest.ThemeID.ToString());
            DatabaseConnect.execSPNonQuery("CopyQuestionsbetweenThemes", parameters);
        }
    }
}
